<?php
/**
 * Created by PhpStorm.
 * User: tangmusen
 * Date: 2017/9/4
 * Time: 9:36
 */

namespace app\admin\controller;


use app\admin\model\Bet;
use app\admin\model\Odds;
use Qiniu\Auth;
use think\Cache;
use think\Config;
use think\Request;
use think\Db;

class Room extends Admin
{
    /**
     * 房间列表
     */
    public function index(){
        $data = Db::name('chatroom')
            ->alias('c')
            ->field('c.*,m.nickname')
            ->join('dl_member m','m.id=c.uid')
            ->order('c.id desc')
            ->paginate(20);
        if($data) {
            $list = $data->all();
            foreach ($list as $k => $v) {
                if($v['managers']){
                   $managers = json_decode($v['managers'],true);
                   $managers_str = implode(',',$managers);
                   $v['managers_str'] = $managers_str;
                   $data[$k] = $v;
                }
            }
        }
        return view('',[
            'page'=>$data->render(),
            'data'=>$data,
        ]);
    }

    /**
     * 删除房间
     */
    public function delete(Request $request){
        $id      = $request->param('id');
        $result   = Db::name('chatroom')->where(array('id'=>$id))->delete();
        if ($result) {
            $this->success_new('删除成功',url('room/index'));
        }else{
            $this->error_new('删除失败');
        }
    }

    /**
     * 编辑房间
     */
    public function edit(Request $request){
        if($request->isPost()){
            $data = $request->post();
            $map=array(
                'id'=>$data['id']
            );
            $result=Db::name('chatroom')->where($map)->update($data);
            if ($result) {
                $this->success_new('修改成功');
            }else{
                $this->error_new('修改失败');
            }
        }else{
            $qiniu_config = config('qiniu');
            $accessKey    = $qiniu_config['ACCESSKEY'];
            $secretKey    = $qiniu_config['SECRETKEY'];
            $auth         = new Auth($accessKey, $secretKey);
            $token        = $auth->uploadToken($qiniu_config['bucket']);
            $domain        = $qiniu_config['domain'];
            $id     = $request->param('id');
            $data   = Db::name('chatroom')->where(array('id'=>$id))->find();
            return view('edit',[
                'data'  =>$data,
                'token' =>$token,
                'domain'=>$domain
            ]);
        }
    }

    /**
     * 创建房间审核
     */
    public function create_list(){
        $data = Db::name('apply')
            ->alias('a')
            ->field('a.*,m.nickname,m.mobile')
            ->join('dl_member m','m.id=a.uid')
            ->where('a.way',2)
            ->order('a.id desc')
            ->paginate(20);
        return view('',[
            'page'=>$data->render(),
            'data'=>$data,
        ]);
    }

    /**
     * 申请发言列表
     */
    public function say_list(){
        $data = Db::name('apply')
            ->alias('a')
            ->field('a.*,m.nickname,m.mobile')
            ->join('dl_member m','m.id=a.uid')
            ->where('a.way',1)
            ->order('a.id desc')
            ->paginate(20);
        return view('',[
            'page'=>$data->render(),
            'data'=>$data,
        ]);
    }

    /**
     * 审核创建房间
     */
    public function edit_create_room_status(Request $request){
        $uid  = $request->post('uid');
        $id   = $request->post('id');
        $type = $request->post('type');
        if($type==2){
            $up0 = [
                'status'=>2
            ];
            Db::name('apply')->where('id',$id)->update($up0);
            $up1 = [
                'is_room'=>1
            ];
            Db::name('member')->where('id',$uid)->update($up1);
        }
        if($type==3){
            $up0 = [
                'status'=>3
            ];
            Db::name('apply')->where('id',$id)->update($up0);
        }
        json_return(200,'成功');
    }

    /**
     * 审核发言
     */
    public function edit_say_status(Request $request){
        $uid  = $request->post('uid');
        $id   = $request->post('id');
        $type = $request->post('type');
        if($type==2){
            $up0 = [
                'status'=>2
            ];
            Db::name('apply')->where('id',$id)->update($up0);
            $up1 = [
                'is_say'=>1
            ];
            Db::name('member')->where('id',$uid)->update($up1);
        }
        if($type==3){
            $up0 = [
                'status'=>3
            ];
            Db::name('apply')->where('id',$id)->update($up0);
        }
        json_return(200,'成功');
    }



}